package com.example.common;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public abstract class Fragment extends androidx.fragment.app.Fragment {

    protected View mView;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        //初始化参数
        initArgs(getArguments());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(mView==null){
            int layId=getContentLayoutId();
            //初始化当前根布局
            View view = inflater.inflate(layId,container,false);

            mView = view;
        }
        else{
            if(mView.getParent()!=null){
                //把当前view从其父布局中移除
                ((ViewGroup)mView.getParent()).removeView(mView);
            }
        }


        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //当view创建完成后初始化数据
        init();
    }

    /**
     * 初始化当前参数
     */
    protected void initArgs(Bundle bundle){

    }

    /**
     * 得到当前界面资源ID
     * @return 资源文件id
     */
    protected abstract int getContentLayoutId();

    /**
     * 初始化
     */
    protected void init(){

    }


    /**
     * 返回按键触发时调用
     * @return Ture代表已经处理烦恼会逻辑 Activity不用finish
     * False代表未处理
     */
    public boolean onBackPressed(){
        return false;
    }
}
